package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.Constant;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.custom.RoundedImageView;
import com.luck.easyfoodbatch.glide.GlideCircleTransform;
import com.luck.easyfoodbatch.mvp.mine.CollectModel;
import com.luck.easyfoodbatch.mvp.mine.CollectPresenter;
import com.luck.easyfoodbatch.mvp.mine.CollectView;
import com.luck.easyfoodbatch.mvp.model.Collect;
import com.luck.easyfoodbatch.mvp.model.SearchGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.XLHRatingBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopIntroductionActivity extends BaseActivity implements CollectView {

    @BindView(R.id.iv_icon)
    RoundedImageView ivIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.shop_rating)
    TextView shopRating;
    @BindView(R.id.tv_follow)
    TextView tvFollow;
    @BindView(R.id.rl_follow)
    RelativeLayout rlFollow;
    @BindView(R.id.rl_shop)
    RelativeLayout rlShop;
    @BindView(R.id.tv_rate)
    TextView tvRate;
    @BindView(R.id.tv_shop_openline)
    TextView tvShopOpenline;
    @BindView(R.id.tv_qualifications)
    TextView tvQualifications;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.rl_shop_name)
    RelativeLayout rlShopName;
    @BindView(R.id.ratingBar3)
    XLHRatingBar ratingBar3;
    private SearchGoods.DataBean.ShopBean shop;
    private int is_collect = 0;
    private CollectModel collectModel;
    private String shop_id = "";
    protected String toChatUsername;
    protected String toChatNickname;
    private String p_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_introduction, "店铺简介", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        p_id = (String) SharedPreferencesUtil.getData(ShopIntroductionActivity.this, HttpUtil.P_ID, "3");
        shop = (SearchGoods.DataBean.ShopBean) getIntent().getSerializableExtra("shop");
        is_collect = getIntent().getIntExtra("is_collect", 0);
        shop_id = getIntent().getStringExtra("shop_id");
        toChatUsername = getIntent().getStringExtra(Constant.EXTRA_USER_ID);
        toChatNickname = getIntent().getStringExtra(Constant.EXTRA_NICK_ID);
        String good_comment = shop.getGood_comment();
        if (!Tools.isNull(good_comment)) {
            double v = Double.parseDouble(good_comment);
            int score = (int) (v * 100);
            tvRate.setText(score + "%");
        } else {
            tvRate.setText("0%");
        }
        if (!Tools.isNull(shop.getCreated_at())) {
            tvShopOpenline.setText(Tools.SubTime2(shop.getCreated_at() + ""));
        } else {
            tvShopOpenline.setText("1970-01-01");
        }
        int is_shoper = shop.getIs_shoper();
        //保证金1未交2已交
        switch (is_shoper) {
            case 3:
            case 6:
                tvQualifications.setText("已缴纳保证金");
                break;
            default:
                tvQualifications.setText("未缴纳保证金");
                break;
        }
        ratingBar3.setCountSelected(shop.getScore());
        String name = shop.getName();
        tvShopName.setText(name);
        tvName.setText(shop.getShop_name());
        String shop_avatar = shop.getShop_avatar();
        if (!Tools.isNull(shop_avatar)) {
            Glide.with(getApplicationContext())
                    .load(HttpUtil.IMG_BASE_URL + shop_avatar)
                    .centerCrop()
                    .placeholder(R.mipmap.loading_4b3)
                    .transform(new GlideCircleTransform(mContext))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(ivIcon);
        } else {
            ivIcon.setImageResource(R.mipmap.loading_4b3);
        }

        //0未收藏1已收藏
        switch (is_collect) {
            case 0:
                tvFollow.setText("收藏");
                Tools.modifyTextViewDrawable(mContext, tvFollow, R.mipmap.guanzhu02_def_1x, 0);
                break;
            case 1:
                tvFollow.setText("已收藏");
                Tools.modifyTextViewDrawable(mContext, tvFollow, R.mipmap.guanzhu02_sel_1x, 0);
                break;
        }

    }

    @OnClick({R.id.rl_follow, R.id.rl_shop, R.id.rl_shop_name})
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.rl_follow:
                showProgressDialog(getString(R.string.app_loading));
                collectModel = new CollectPresenter(ShopIntroductionActivity.this, mContext);
                collectModel.collect("2", shop_id, p_id);
                break;
            case R.id.rl_shop:
                finish();
                break;
            case R.id.rl_shop_name:
                if (isCheckedLogin()) {
                    bundle.putString(Constant.EXTRA_USER_ID, toChatUsername);
                    bundle.putString(Constant.EXTRA_NICK_ID, shop.getShop_name());
                    startAct(ChatActivity.class, bundle);
                } else {
                    startAct(LoginActivity.class);
                }
                break;
        }
    }

    @Override
    public void collectSuccess(Collect model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                String msg = model.getMsg();
                if (!Tools.isNull(msg) && msg.equals("收藏成功")) {
                    tvFollow.setText("已收藏");
                    Tools.modifyTextViewDrawable(mContext, tvFollow, R.mipmap.guanzhu02_sel_1x, 0);
                } else if (!Tools.isNull(msg) && msg.equals("取消成功")) {
                    tvFollow.setText("收藏");
                    Tools.modifyTextViewDrawable(mContext, tvFollow, R.mipmap.guanzhu02_def_1x, 0);
                }
                T.showToast(mContext, msg);
            } else {
                T.showToast(mContext, model.getMsg());
            }
            sendBroadcast("action.app.refresh.shop");
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }


}
